package Server;

import java.net.*;
import java.io.*;
import java.lang.*;

import Protocol.Command;
import Protocol.Package;

// class decleration of connection
class Connection extends Thread {

	Socket socket = null; // the actual socket connection
	public boolean is_connected = false; // the connection
	ServerSocket server = null; // the socket of the GameServer

	// constructor method for class connection
	Connection(ServerSocket server_sock) {

		server = server_sock;
	}

	// returns whether or not the connection is connected to the server
	boolean connected() {

		return is_connected;
	}

	// returns the opened socket
	Socket get_socket() {
		is_connected = false;
		return socket; // return the opened socket
	}

	// part that actually runs. Attempts to obtain a new socket
	public void run() {

		while (true) {
			System.out.println("Running");
			try {
				socket = server.accept();
			} catch (IOException e) {
				System.out.println("execption " + e.toString());
			}
			System.out.println(socket.getInetAddress().getHostName());
			networksocket client = new networksocket(socket);
			String buf = client.read_stream();
			if (buf != null) {
				Package pkg = Command.parse(buf);
				Server.taskque.add(pkg);
				Server.clientset.put(pkg.getHost(),socket);
			}
			try {
				Thread.sleep(5);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
